//
//  MRMSdk.m
//  MeiRiMoney
//
//  Created by qiuqk on 13-6-24.
//  Copyright (c) 2013年 MeiRiCai. All rights reserved.
//

#import "MRMConfig.h"
#import "MRMSdk.h"
#import "OAuthConsumer.h"
#import "OauthUtils.h"
#import "MR_Convert.h"

static OAToken *_Token = nil;

@implementation MRMSdk

+ (NSString *)signaturePublicUrl:(NSString *)urlString
{
	NSLog(@"%@", urlString);
	NSURL *url = [[NSURL alloc] initWithString:urlString];
	OAConsumer *consumer = [MRMConfig getConsumer];
	OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url consumer:consumer token:nil realm:nil signatureProvider:nil];
	NSString *signatureUrl = [request getSignatureUrl];
	NSLog(@"%@", signatureUrl);
	return signatureUrl;
}

+ (NSString *)signatureProtectedUrl:(NSString *)urlString
{
	NSLog(@"%@", urlString);
	NSURL *url = [[NSURL alloc] initWithString:urlString];
	OAConsumer *consumer = [MRMConfig getConsumer];
	OAToken *token = [self getToken];
	OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url consumer:consumer token:token realm:nil signatureProvider:nil];
	NSString *signatureUrl = [request getSignatureUrl];
	NSLog(@"%@", signatureUrl);
	return signatureUrl;
}

+ (void) setToken:(NSString *) key withSecret:(NSString *) secret
{
	_Token = [[OAToken alloc] initWithKey:key secret:secret];
}

+ (void) setToken:(LoginResult *)loginResult
{
	if (loginResult != nil)
	{
		NSLog(@"登陆成功, Token=%@, Secret=%@", loginResult.Token, loginResult.Secret);
		[self setToken:loginResult.Token withSecret:loginResult.Secret];
	}
	else
	{
		NSLog(@"%@", @"登陆失败");
	}
}

+ (OAToken *) getToken
{
	if (_Token == nil)
	{
		LoginResult *loginResult = [self login];
		[self setToken:loginResult];
	}
	
	return _Token;
}

+ (LoginResult *)login
{
	NSString *relativePath = @"/MeiRiMoney/Login";
	NSMutableDictionary *queryParams = [NSMutableDictionary dictionary];
    [queryParams setObject:[MRMConfig getDeviceId] forKey: @"deviceid"];
	[queryParams setObject:@"iOS" forKey:@"device"];
	[queryParams setObject:[MRMConfig getConsumerKey] forKey: @"consumerkey"];
	@try
	{
		NSDictionary *dictResult = [OauthUtils getFromPublicUrl:relativePath withDict:queryParams];
		LoginResult *loginResult = [[LoginResult alloc] initWithDictionary:dictResult];
		[self setToken:loginResult];
		
		return loginResult;
	}
	@catch (NSException *exception)
	{
		NSLog(@"%@%@", [exception name], [exception reason]);
	}
	return nil;
}

+ (int)getIntegralBalance
{
	NSString *relativePath = @"/MeiRiMoney/IntegralBalance";
	id result = [OauthUtils getFromProtectedUrl:relativePath withDict:nil resultIsJson:FALSE];
	return ((NSNumber *)result).intValue;
}

+ (NSArray *)getTaskRecords:(int)count page:(int)page
{
	return nil;
}

+ (void)exchangeCommodity:(long)commodityId number:(int)number shipAddress:(NSString *)shipAddress
{
	NSString *relativePath = @"/MeiRiMoney/ExchangeCommodity";
	NSMutableDictionary *queryParams = [NSMutableDictionary dictionary];
    [queryParams setObject:[MR_Convert intToString:commodityId] forKey: @"commodityid"];
	[queryParams setObject:[MR_Convert intToString:number] forKey:@"number"];
	[queryParams setObject:shipAddress forKey:@"shipaddress"];
	[OauthUtils getFromProtectedUrl:relativePath withDict:queryParams resultIsJson:true];
}

+ (GSetting *)getGSetting:(NSString *)key
{
	NSString *relativePath = @"/MeiRiMoney/GSetting";
	NSMutableDictionary *queryParams = [NSMutableDictionary dictionary];
	[queryParams setObject:key forKey:@"key"];
	NSDictionary *dictResult = [OauthUtils getFromPublicUrl:relativePath withDict:queryParams];
	GSetting *gSetting = [[GSetting alloc] initWithDictionary:dictResult];
	return gSetting;
}


@end
